package com.xinghai.system.service.impl;

import java.util.List;
import com.xinghai.common.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.XhDriverCarRelationMapper;
import com.xinghai.system.domain.XhDriverCarRelation;
import com.xinghai.system.service.IXhDriverCarRelationService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 车辆人员关联Service业务层处理
 * 
 * @author smy
 * @date 2024-07-21
 */
@Service
public class XhDriverCarRelationServiceImpl extends ServiceImpl<XhDriverCarRelationMapper, XhDriverCarRelation> implements IXhDriverCarRelationService
{
    @Autowired
    private XhDriverCarRelationMapper xhDriverCarRelationMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询车辆人员关联
     * 
     * @param id 车辆人员关联主键
     * @return 车辆人员关联
     */
    @Override
    public XhDriverCarRelation getInfo(Long id)
    {
        return xhDriverCarRelationMapper.getInfo(id);
    }

    /**
     * 查询车辆人员关联列表
     * 
     * @param xhDriverCarRelation 车辆人员关联
     * @return 车辆人员关联
     */
    @Override
    public List<XhDriverCarRelation> list(XhDriverCarRelation xhDriverCarRelation)
    {
            xhDriverCarRelation.setDelFlag("0");
        return xhDriverCarRelationMapper.list(xhDriverCarRelation);
    }

    /**
     * 新增车辆人员关联
     * 
     * @param xhDriverCarRelation 车辆人员关联
     * @return 结果
     */
    @Override
    public int insert(XhDriverCarRelation xhDriverCarRelation)
    {
        xhDriverCarRelation.setCreateTime(DateUtils.getNowDate());
        return xhDriverCarRelationMapper.insert(xhDriverCarRelation);
    }
    /**
     * 批量新增车辆人员关联
     *
     * @param xhDriverCarRelationList 车辆人员关联
     * @return 结果
     */
    @Override
    public int insertBatch(List<XhDriverCarRelation> xhDriverCarRelationList)
    {
        return xhDriverCarRelationMapper.insertBatch(xhDriverCarRelationList);
    }
    /**
     * 修改车辆人员关联
     * 
     * @param xhDriverCarRelation 车辆人员关联
     * @return 结果
     */
    @Override
    public int update(XhDriverCarRelation xhDriverCarRelation)
    {
        xhDriverCarRelation.setUpdateTime(DateUtils.getNowDate());
        return xhDriverCarRelationMapper.update(xhDriverCarRelation);
    }

    /**
     * 批量删除车辆人员关联
     * 
     * @param ids 需要删除的车辆人员关联主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return xhDriverCarRelationMapper.deleteBatch(ids);
    }

    /**
     * 删除车辆人员关联信息
     * 
     * @param id 车辆人员关联主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return xhDriverCarRelationMapper.delete(id);
    }

    /**
     * 导入车辆人员关联信息
     *
     * @param list 车辆人员关联信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<XhDriverCarRelation> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            XhDriverCarRelationMapper xhDriverCarRelationMapper = sqlSession.getMapper(XhDriverCarRelationMapper.class);
            list.stream().forEach(item -> xhDriverCarRelationMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
